package com.health.db.service.user.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.health.db.dao.user.IUserAccountJournalDao;
import com.health.db.dao.user.IUserAccountLedgerDao;
import com.health.db.dao.user.IUserAccountVoucherBillDao;
import com.health.db.model.UserAccountJournal;
import com.health.db.model.UserAccountLedger;
import com.health.db.model.UserAccountVoucherBill;
import com.health.db.model.UserAccountVoucherBillExample;
import com.health.db.model.UserCashAccountIdNo;
import com.health.db.service.user.IUserAccountVoucherBillService;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;

public class UserAccountVoucherBillServiceImpl implements
		IUserAccountVoucherBillService {
	private IUserAccountVoucherBillDao userAccountVoucherBillDao;
	private IUserAccountJournalDao userAccountJournalDao ;
    private IUserAccountLedgerDao userAccountLedgerDao;
	
	
	public IUserAccountLedgerDao getUserAccountLedgerDao() {
		return userAccountLedgerDao;
	}
	public void setUserAccountLedgerDao(IUserAccountLedgerDao userAccountLedgerDao) {
		this.userAccountLedgerDao = userAccountLedgerDao;
	}
	/**
	 * @return the userAccountJournalDao
	 */
	public IUserAccountJournalDao getUserAccountJournalDao() {
		return userAccountJournalDao;
	}
	/**
	 * @param userAccountJournalDao the userAccountJournalDao to set
	 */
	public void setUserAccountJournalDao(
			IUserAccountJournalDao userAccountJournalDao) {
		this.userAccountJournalDao = userAccountJournalDao;
	}
	public IUserAccountVoucherBillDao getUserAccountVoucherBillDao() {
		return userAccountVoucherBillDao;
	}
	public void setUserAccountVoucherBillDao(
			IUserAccountVoucherBillDao userAccountVoucherBillDao) {
		this.userAccountVoucherBillDao = userAccountVoucherBillDao;
	}
	@Override
	public int insertUserAccountVoucherBill(UserAccountLedger  userAccountLedger,UserAccountVoucherBill userAccountVoucherBill) {
		// TODO Auto-generated method stub
		//SqlSession sqlSession = factory.openSession();
		int result=-1;
		//加锁
		if (userAccountVoucherBillDao.Locked(userAccountLedger.getAccountid(), 0)) {
			Long userId = userAccountLedger.getAccountid();
			Date now = DateUtils.now();
			
			userAccountVoucherBill.setBillNo(DateUtils.todayDate()+"B");
			userAccountVoucherBill.setRunStatus("草稿");
			userAccountVoucherBill.setUserid(userId);
			userAccountVoucherBill.setUserName(userAccountLedger.getAccountName());
			
			UserCashAccountIdNo userCashAccountIdNo =userAccountLedger.getUserCashAccountIdNo() ;
			userAccountVoucherBill.setAccountid(userCashAccountIdNo.getAccountid());
			userAccountVoucherBill.setAccounttypeid((short) userCashAccountIdNo.getAccounttypeid().intValue());
			userAccountVoucherBill.setAccountTypeName(userCashAccountIdNo.getAccountTypeName());
			userAccountVoucherBill.setAccountBankName(userCashAccountIdNo.getAccountBankName());
			userAccountVoucherBill.setAccountIdno(userCashAccountIdNo.getAccountIdno());
			userAccountVoucherBill.setAccountName(userCashAccountIdNo.getAccountName());
			userAccountVoucherBill.setAccountIdentityid(userCashAccountIdNo.getAccountIdentityid());
			userAccountVoucherBill.setAccountContactPhone(userCashAccountIdNo.getContactPhone());
			userAccountVoucherBill.setUpdateUserid(userAccountLedger.getAccountid());
			userAccountVoucherBill.setSystemid(0);
			userAccountVoucherBill.setCreateDate(now);
			userAccountVoucherBill.setUpdateDate(now);
			
		
			result= userAccountVoucherBillDao.insert(userAccountVoucherBill);
			
			//日记账记录
			if (result==1) {
				UserAccountJournal accountJournal = new UserAccountJournal();
				accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_11);
				accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_11);
				
				accountJournal.setAcctpageid(userAccountLedger.getAcctpageid());
				accountJournal.setAccountid(userId);
				accountJournal.setAccountIdno(userAccountLedger.getAccountIdno());
				accountJournal.setAccountName(userAccountLedger.getAccountName());
				accountJournal.setVoucherid(userAccountVoucherBill.getBillid().toString());
				accountJournal.setVoucherIdno(userAccountVoucherBill.getBillNo()+userAccountVoucherBill.getBillid());
				accountJournal.setJournalTotal(userAccountVoucherBill.getBillTotal());
				
				accountJournal.setCreateUserid(userId);
				accountJournal.setUpdateUserid(userId);
				accountJournal.setVoucherDate(now);
				accountJournal.setVouchertypeid(Constant.CONSTANTS_PAY_ACTION_ID_22);
				accountJournal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_21);
	
				userAccountJournalDao.insert(accountJournal);
			}
			//解锁
			userAccountVoucherBillDao.Locked(userAccountLedger.getAccountid(), 1);
			
		}else{
			
			result=-2;
		}
		return result;
	}
	
	
	/**
	 * 取现申请驳回
	 */
	@Override
	public int cancelAccountVoucherBill(Long docId ,Long userId) {
		
		// TODO Auto-generated method stub
		//SqlSession sqlSession = factory.openSession();
		
		int result=-1;
		UserAccountLedger  userAccountLedger = userAccountLedgerDao.selectUserAccountLedgerByUserId(docId);
		UserAccountVoucherBillExample example = new UserAccountVoucherBillExample();
		example.setUserid(docId);
		example.setRunFlag((short)0);
		List<UserAccountVoucherBill> list = userAccountVoucherBillDao.selectByExample(example);
		
		
		if (list.size()>0) {
			UserAccountVoucherBill	temp = list.get(0);
			UserAccountVoucherBill userAccountVoucherBill=new UserAccountVoucherBill();
			userAccountVoucherBill.setRecid(temp.getRecid());
			userAccountVoucherBill.setRunStatus("已关闭");
			userAccountVoucherBill.setRunFlag((short) 8);
			userAccountVoucherBill.setUpdateUserid(userId);
			result =  userAccountVoucherBillDao.updateByPrimaryKeySelective(userAccountVoucherBill);
			
			
			UserAccountJournal accountJournal = new UserAccountJournal();
			accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_13);
			accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_11);
			
			accountJournal.setAcctpageid(userAccountLedger.getAcctpageid());
			accountJournal.setAccountid(temp.getUserid());
			accountJournal.setAccountIdno(userAccountLedger.getAccountIdno());
			accountJournal.setAccountName(userAccountLedger.getAccountName());
			accountJournal.setVoucherid(temp.getBillid().toString());
			accountJournal.setVoucherIdno(temp.getBillNo());
			accountJournal.setJournalTotal(temp.getBillTotal());
			
			accountJournal.setCreateUserid(userId);
			accountJournal.setUpdateUserid(userId);
			accountJournal.setVoucherDate(new Date());
			accountJournal.setVouchertypeid(Constant.CONSTANTS_PAY_ACTION_ID_23);
			accountJournal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_21);
			userAccountJournalDao.deleteByBillId(temp.getBillid(), temp.getUserid());
			userAccountJournalDao.insert(accountJournal);
			
		}
		return result;
	}
	
	@Override
	public int updateAccountVoucherBill(Long docId ,Long userId) {
		
		// TODO Auto-generated method stub
		//SqlSession sqlSession = factory.openSession();
		
		int result=-1;
		UserAccountLedger  userAccountLedger = userAccountLedgerDao.selectUserAccountLedgerByUserId(docId);
		UserAccountVoucherBillExample example = new UserAccountVoucherBillExample();
		example.setUserid(docId);
		example.setRunFlag((short)0);
		List<UserAccountVoucherBill> list = userAccountVoucherBillDao.selectByExample(example);
		
		
		if (list.size()>0) {
			UserAccountVoucherBill	temp = list.get(0);
			UserAccountVoucherBill userAccountVoucherBill=new UserAccountVoucherBill();
			userAccountVoucherBill.setRecid(temp.getRecid());
			userAccountVoucherBill.setRunStatus("已完成");
			userAccountVoucherBill.setRunFlag((short) 9);
			userAccountVoucherBill.setUpdateUserid(userId);
			result =  userAccountVoucherBillDao.updateByPrimaryKeySelective(userAccountVoucherBill);
			
			UserAccountLedger  userAccountLedgerForUpdate = new UserAccountLedger();
			userAccountLedgerForUpdate.setAcctpageid(userAccountLedger.getAcctpageid());
			userAccountLedgerForUpdate.setBalanceTotal(userAccountLedger.getBalanceTotal().subtract(temp.getBillTotal()));
			
			userAccountLedgerForUpdate.setAccountid(userAccountLedger.getAccountid());
			
			userAccountLedgerDao.updatAccountLedgerByUserIdSelective(userAccountLedgerForUpdate);
			
			UserAccountJournal accountJournal = new UserAccountJournal();
			accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_12);
			accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_11);
			
			accountJournal.setAcctpageid(userAccountLedger.getAcctpageid());
			accountJournal.setAccountid(temp.getUserid());
			accountJournal.setAccountIdno(userAccountLedger.getAccountIdno());
			accountJournal.setAccountName(userAccountLedger.getAccountName());
			accountJournal.setVoucherid(temp.getBillid().toString());
			accountJournal.setVoucherIdno(temp.getBillNo());
			accountJournal.setJournalTotal(temp.getBillTotal().multiply(new BigDecimal(-1)));
			
			accountJournal.setCreateUserid(userId);
			accountJournal.setUpdateUserid(userId);
			accountJournal.setVoucherDate(new Date());
			accountJournal.setVouchertypeid(Constant.CONSTANTS_PAY_ACTION_ID_21);
			accountJournal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_21);
			userAccountJournalDao.deleteByBillId(temp.getBillid(), temp.getUserid());
			userAccountJournalDao.insert(accountJournal);
		}
		return result;
	}
	
	@Override
	public int countUserAccountVoucherBillSimple(
			UserAccountVoucherBill userAccountVoucherBill) {
		int result=-1;
		result= userAccountVoucherBillDao.countUserAccountVoucherBillSimple(userAccountVoucherBill);
		return result;
	}
	
}
